package com.fyy.kafuka.partition;

import org.apache.kafka.clients.producer.Partitioner;
import org.apache.kafka.common.Cluster;
import org.apache.kafka.common.PartitionInfo;

import java.util.List;
import java.util.Map;

/**
 * @Author fyy
 * @Description
 * @Date 2020/10/22 15:31
 */
public class MyPartiton implements Partitioner {


    @Override
    public int partition(String topic, Object key, byte[] keyBytes, Object value, byte[] valueBytes, Cluster cluster) {
        //自定义的轮询策略
        Integer numPartition = cluster.partitionCountForTopic(topic); //获取集群分区数
        List<PartitionInfo> partitionInfos = cluster.availablePartitionsForTopic(topic);//获取集群可用分区数
        int num = partitionInfos.size();
        return keyBytes.toString().hashCode() % num;
    }

    @Override
    public void close() {

    }

    @Override
    public void configure(Map<String, ?> map) {

    }
}
